package mk.gamemap.model
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	
	import mk.gamemap.model.gameevent.DoorEvt;
	import mk.gamemap.model.gameevent.GEvent;
	import mk.gamemap.model.gameevent.MapEvtFactory;
	import mk.gamemap.view.sprite.CharView;
	import mk.rpg.World;
	
	import mx.events.DynamicEvent;
	import game.tileMap.model.vo.MapObj;
	import game.tileMap.model.vo.Character;

	public class Director extends EventDispatcher
	{
		public var mapModel:TileMapModel0
		public var player:CharView
		private var eventDict:Dictionary
		private var world:World
		private var evtFactory:MapEvtFactory
		
		public var running:Boolean;
		public function Director(world:World)
		{
			this.world=world;
			eventDict=new Dictionary();
			evtFactory=new MapEvtFactory();
			
		}
		
		public function setup():void{
			for each(var mapSp:MapObj in mapModel.mapSprites){
				if (mapSp is Character){
					mapSp.addEventListener(Character.TOUCH_SPRITE,touchEvent);
				}
				var evt:GEvent=MapEvtFactory.creatEvent(mapSp)
					if (evt){
						evt.addEventListener(GEvent.EVENT_COMMAND,exeCommand);
						eventDict[mapSp]=evt;
					}
				
			}
		}
		
		public function checkEvent():void{
			check_event_trigger_there()
		}
		
		public function update():void{
			if (!running){
				for each(var gevt:GEvent in eventDict){
					if (gevt is GEvent){
						gevt.update();
					}
					
					
				}
			}
		}
		
		public function check_event_trigger_there():void{
			
			
			var front_x:int = player.X
			var front_y:int = player.Y-1
				var mapSp:MapObj=mapModel.getSp_xy(front_x,front_y);
			if (mapSp!=null){
				//trace("事件接触执行！")
//				for each(var evt:DoorEvt in mapModel.mapSprites){
//					var evt:DoorEvt=new DoorEvt(mapSp);
//				}
				//var evt:GEvent=eventDict[mapSp]
				//evt.start()
			}
			
		}
		
		public function pause():Boolean{
			if (world.uiManager.msgWindow.visible){
				return true
			}
			
			return false;
		}
		
		private function touchEvent(evt:DynamicEvent):void{
			if (!running){
				var mapSp:MapObj=evt.touchSp;
				
				var gevt:GEvent=eventDict[mapSp]
				trace(gevt+"事件接触执行！"+mapSp)
				if (gevt){
					trace(gevt+"car事件接触执行！")
					gevt.start()
					
				}
			}
			
			
			
			
		}
		
		private function exeCommand(evt:DynamicEvent):void{
			
			world.uiManager.msgWindow.speak(evt.txt);
			world.keyCtrl.isCtrl=false;
			running=true;
			
		}
		
		public function changeMap(id:String):void{
			dispose();
			world.worldModelBox.mapModel.setup(id);
			world.mapView.drawMap();
			setup();
		}
		
		public function dispose():void{
			for each(var mapSp:MapObj in mapModel.mapSprites){
				if (mapSp is Character){
					mapSp.removeEventListener(Character.TOUCH_SPRITE,touchEvent);
				}
//				var evt:GEvent=MapEvtFactory.creatEvent(mapSp)
//				if (evt){
//					evt.addEventListener(GEvent.EVENT_COMMAND,exeCommand);
//					eventDict[mapSp]=evt;
//				}
				
			}
		}
	}
}